# Maintainer: Felix Xing (Felixaszx) <felixaszx@outlook.com>

_realname=fltk
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.4.3
pkgrel=1
pkgdesc="C++ user interface toolkit (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.fltk.org/"
msys2_repository_url='https://github.com/fltk/fltk'
license=('spdx:LGPL-2.0-or-later WITH FLTK-exception')
depends=("${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools")
source=("https://github.com/${_realname}/${_realname}/archive/release-${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-fltk-config-uuid-shared.patch"
        "002-fix-cmake-config-install-destination.patch")
sha256sums=('6a11c0bf91b7b193a87a1928c32a953f36d7dd4b65fef3e9d0c40a51882f97a6'
            '30ea78d49d15dcc6cb567ab08d0c13f0ddcb1844dc9b22ebcd3bd80dd88f640e'
            '96825fb284f86b3bd9c7872b15dbdafd66932435094f4287ffc9d159e2b52c22')

prepare() {
  cd "${srcdir}/${_realname}-release-${pkgver}"
  patch -p1 -i "${srcdir}"/001-fltk-config-uuid-shared.patch
  patch -p1 -i "${srcdir}"/002-fix-cmake-config-install-destination.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    CXXFLAGS+=" -Wno-c++11-narrowing"
  else
    CXXFLAGS+=" -Wno-narrowing"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -Wno-dev \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    "${_extra_config[@]}" \
    -DFLTK_BUILD_TEST=OFF \
    -DFLTK_USE_SYSTEM_LIBJPEG=ON \
    -DFLTK_USE_SYSTEM_LIBPNG=ON \
    -DFLTK_USE_SYSTEM_ZLIB=ON \
    -DFLTK_BUILD_SHARED_LIBS=ON \
    -DFLTK_GRAPHICS_GDIPLUS=OFF \
    "../${_realname}-release-${pkgver}"

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./

  install -Dm644 "${srcdir}/${_realname}-release-${pkgver}/COPYING" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/share/fltk/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}/bin/fltk-config"
}
